package cn.wzl.recognition;

import cn.wzl.recognition.bpnn.PictureRecognitionBPNN;
import cn.wzl.recognition.bpnn.util.PictureRecognitionConverter;
import cn.wzl.recognition.mvc.Controller;
import cn.wzl.recognition.mvc.Model;
import cn.wzl.recognition.mvc.View;
import cn.wzl.recognition.training.CVSFileLoader;
import cn.wzl.recognition.training.Trainer;

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {
        PictureRecognitionBPNN bp = new PictureRecognitionBPNN(new int[]{28*28, 35, 10}, 0.4, 0.000001, 0.5, true);
        Trainer trainer = retrieveTrainer(bp);
        show(bp, trainer, "PictureRecognitionDemo", 480, 400, true);
    }

    private static Trainer retrieveTrainer(PictureRecognitionBPNN bp) throws IOException {
        System.out.println("loading training data.");
        CVSFileLoader labels = new CVSFileLoader("src/main/resources/training-data/qmnist-train-labels.csv");
        CVSFileLoader images = new CVSFileLoader("src/main/resources/training-data/qmnist-train-images.csv");
        return new Trainer(
                PictureRecognitionConverter.formatData(images.getData()),
                PictureRecognitionConverter.formatLabels(labels.getData()),
                labels.getData(),
                labels.retrieveRowSize(),
                bp
        );

    }

    @SuppressWarnings("SameParameterValue")
    private static void show(PictureRecognitionBPNN bp, Trainer trainer, String title, int width, int height, boolean isDebugMode) {
        new Controller(
                new Model(
                        new View(title, width, height),
                        bp,
                        trainer,
                        isDebugMode
                )
        );
    }

}
